package cn.com.fuhuitong.main.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import cn.com.fuhuitong.main.home.entity.MemberEntity
import cn.com.fuhuitong.utils.SpanUtils
import com.alibaba.android.vlayout.LayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.utils.WebViewUtils
import kotlinx.android.synthetic.main.item_member_benefit.view.*
import kotlinx.android.synthetic.main.item_member_benefit_content.view.*
import kotlinx.android.synthetic.main.item_member_content.view.*

enum class MVT(value: Int) {
    MEMBER_LIST(1),
    MEMBER_CONTENT(2)
}

class MemberListAdapter(
    private val mContext: Context,
    private val mHelper: LayoutHelper
) : BaseDelegateAdapter<MemberEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_member_benefit,
    mItemViewType = MVT.MEMBER_LIST.ordinal
) {

    data class BenefitBean(
        @DrawableRes
        val icon: Int = 0,
        val title: String,
        val message: String
    )

    private val benefit_1: MutableList<BenefitBean> = mutableListOf(
        BenefitBean(
            R.drawable.ic_benefit_7, "体验20次按摩椅", "价值￥1600.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_2, "浓姜汤姜素各1份 ", "价值￥396.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_3, "购物无忧", "赠送999积分"
        ), BenefitBean(
            R.drawable.ic_benefit_4, "低价采购", "商城采购/代理权"
        ), BenefitBean(
            R.drawable.ic_benefit_5, "公益培训", "1个名额"
        ), BenefitBean(
            R.drawable.ic_benefit_6, "升级家庭驿站", "累计推广25个"
        )
    )

    private val benefit_2: MutableList<BenefitBean> = mutableListOf(
        BenefitBean(
            R.drawable.ic_benefit_11, "独享招商权益", ""
        ), BenefitBean(
            R.drawable.ic_benefit_10, "指导服务", "“驿站运营指导"
        ), BenefitBean(
            R.drawable.ic_benefit_9, "5G智能科技", "身心灵健康调理方案"
        ), BenefitBean(
            R.drawable.ic_benefit_7, "智能按摩椅1台", "价值￥49800.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_2, "浓姜汤姜素各2份", "“价值￥792.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_4, "低价采购", "商城采购/代理权"
        )
//        , BenefitBean(
//            R.drawable.ic_benefit_6, "升级旗舰店代理", "累计推广10个"
//        )
    )

    private val benefit_3: MutableList<BenefitBean> = mutableListOf(
//        BenefitBean(
//            R.drawable.ic_benefit_11, "独享招商权益", "30%的招商加盟店补"
//        ),
        BenefitBean(
            R.drawable.ic_benefit_10, "指导服务", "“驿站运营指导"
        ), BenefitBean(
            R.drawable.ic_benefit_9, "5G智能科技", "身心灵健康调理方案"
        ), BenefitBean(
            R.drawable.ic_benefit_7, "智能按摩椅7台", "价值￥348600.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_5, "手法高级班名额1", "价值￥6800.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_5, "身心智慧班名额1个", "价值￥19800.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_2, "浓姜汤姜素各14份", "价值￥5540.00元"
        ), BenefitBean(
            R.drawable.ic_benefit_4, "低价采购", "商城采购/代理权"
        ),
        BenefitBean(
            R.drawable.ic_benefit_11, "独享招商权益", ""
        )
//        BenefitBean(
//            R.drawable.ic_benefit_6, "30%的招商加盟店补", "5G全民健康智能商城"
//        )
    )

    init {
        addChildClickViewIds(R.id.linear_member_open)
    }

    override fun onBindViewHolder(itemView: View, position: Int, itemData: MemberEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_member_name.text = itemData.title
        itemView.text_member_price.text = SpanUtils.setMoneyStyle(itemData.price.toDouble())

        val layoutManager = GridLayoutManager(mContext, 3)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (itemData.id == 2) if (position == 0) 3 else 1 else 1
            }
        }
        with(itemView.recycler_member_content) {
            this.layoutManager = layoutManager
            this.adapter = DetailsLabelAdapter(
                mContext,
                if (itemData.id == 1) benefit_1 else if (itemData.id == 2) benefit_2 else benefit_3
            )
        }
    }


    class DetailsLabelAdapter(
        private val mContext: Context,
        private val labelName: List<BenefitBean>
    ) :
        RecyclerView.Adapter<RecyclerViewHolder>() {

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): RecyclerViewHolder {
            return RecyclerViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_member_benefit_content, parent, (false))
            )
        }

        override fun getItemCount(): Int {
            return labelName.size
        }

        override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
            with(holder.itemView.image_member_benefit_icon) {
                setImageResource(labelName[position].icon)
            }
            with(holder.itemView.text_member_benefit_name) {
                text = labelName[position].title
            }
            with(holder.itemView.text_member_benefit_message) {
                text = labelName[position].message
            }
        }
    }
}

class MemberContentAdapter(
    private val mContext: Context,
    private val mHelper: LayoutHelper
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_member_content,
    mItemViewType = MVT.MEMBER_CONTENT.ordinal
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: String) {
        super.onBindViewHolder(itemView, position, itemData)
//        itemView.web_member_content.loadDataWithBaseURL(
//            null, itemData, "text/html", "UTF-8", null
//        )
        WebViewUtils.loadFullScreenHtml(itemView.web_member_content, itemData)
    }
}